﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace ConquerServer_Basic
{
    public delegate int ConquerCallback(IBaseEntity sender, IBaseEntity caller);

    public class Kernel
    {
        public static Dictionary<uint, AuthClient> AuthPool = new Dictionary<uint, AuthClient>();
        public static Dictionary<uint, GameClient> GamePool = new Dictionary<uint, GameClient>();

        public static Dictionary<ushort, Dictionary<uint, INpc>> Npcs = new Dictionary<ushort, Dictionary<uint, INpc>>();
        public static Dictionary<uint, Monster> Mobs = new Dictionary<uint, Monster>();
        public static Dictionary<uint, Entity> eMonsters = new Dictionary<uint, Entity>();
        public static Dictionary<uint, MonsterSpawn> MobSpawns = new Dictionary<uint, MonsterSpawn>();
        //public static Dictionary<ushort, Dictionary<ushort, Skill>> Skills = new Dictionary<ushort, Dictionary<ushort, Skill>>();
        //public static Dictionary<ushort, Guild> Guilds = new Dictionary<ushort, Guild>();

        public static GameClient[] Clients = new GameClient[0];
        public static Random Random = new Random();

        public static void UpdateGameClients()
        {
        Update:
            try
            {
                Clients = GamePool.ThreadSafeValueArray<uint, GameClient>();
            }
            catch (IndexOutOfRangeException)
            {
                Kernel.WriteLine("[Database SQL Failure] Failed to update game clients\r\n");
                goto Update;
            }
        }

        private static StringWriter DebugLog = new StringWriter();
        public static void WriteLine(string Line)
        {
            lock (DebugLog)
            {
                Console.WriteLine(Line);
                DebugLog.WriteLine(Line);
                File.WriteAllText(Database.DatabasePath + "\\Debug.log", DebugLog.ToString());
            }
        }

        public static short GetDistance(ushort X, ushort Y, ushort X2, ushort Y2)
        {
            return (short)Math.Max(Math.Abs(X - X2), Math.Abs(Y - Y2));
        }
        public static bool IsItemType(uint ID, ushort Type)
        {
            return ((int)(ID / 1000) == Type);
        }
    }
}
